/*
 * @Author: liuying 247220246@qq.com
 * @Date: 2023-11-22 16:28:49
 * @LastEditors: liuying 247220246@qq.com
 * @LastEditTime: 2024-01-22 14:55:43
 * @FilePath: \ele-h5-server\src\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const path = require("path");
const jsonServer = require("json-server");
const router = require("./router");
const db = require("./db")();
const server = jsonServer.create();
const middlewares = jsonServer.defaults({
  static: path.join(__dirname, "../public"),
});

server.use(middlewares);
// req.body
server.use(jsonServer.bodyParser);
server.use((req, res, next) => {
  let json = res.json.bind(res);
  res.success = (data) => {
    return json({
      code: 0,
      msg: "请求成功",
      data,
    });
  };
  res.fail = (msg, code = -1, data) => {
    return json({
      code,
      msg,
      data,
    });
  };
  next();
});

router(server);
const jsonRouter = jsonServer.router(db);
server.use((req, res, next) => {
  setTimeout(next, 1000);
});
server.use("api", jsonRouter);
server.listen(8000, () => {
  console.log("JSON server listening is running at 8000");
});
